<?php
	//Require configuration for outside enviornment
	require('/var/www/vhosts/iwsdev.com/eden/freeway/admin/includes/configure.php');
/*
*   The WebService_base class needs to enable a connection to the Freeway DB
*   all web services API classes should extend this class.
*  
*   The WebService constructor will assign variables necessary for creation of the WSDL file 
*   and it will include the requested API class. 
* 		- the constructors requires a _REQUEST variable specified by const ws_request.
*        $_REQUEST[self::ws_request]
*   
*/
class WebService_base{
	public $ws_request = 'service'; //-------> 	Parameter used to request web service operation
	const ws_app_dir = 'custom/webservices/';
	const ws_server = 'server.php';
	const ws_wsdl = 'index.php';
	const oe_database_functions = 'database.php';
	const oe_password_funcs = 'password_funcs.php';
	const oe_events_database_tables = 'events_database_tables.php';
	const ws_api_dir = 'WSAPI/';
	const ws_wsdl_gen_script = 'WSDLCreator/WSDLCreator.php';
	const ws_prefix = 'ws_';
	const ws_host = HTTP_SERVER;
	
	public $load; 
	public $oe_includes_path;
	public $db;
	public $load_class_file;
	public $ws_path_sys;
	public $ws_path_url;
	
	/**
	 * Returns the name of the var used in the url request
	 *
	 * @return string
	 */
	function wsdl_request(){
		return $this->ws_request;
	}
	/**
	 * Returns the name of the script that generates the wsdl
	 *
	 * @return string
	 */
	function wsdl_script(){
		return self::ws_wsdl_gen_script;
	}
	/**
	 * Returns the name of the file called to display the wsdl
	 *
	 * @return string
	 */
	function wsdl_page(){
		return self::ws_wsdl;
	}
	/**
	 * Returns the host name of the wsld doc and soap server
	 *
	 * @return string
	 */
	function wsdl_host(){
		return self::ws_host;
	}
	/**
	 * Returns file name of the SOAP server
	 *
	 * @return string
	 */
	function wsdl_srvr(){
		return self::ws_server;
	}
	
	/**
	 * Constructor
	 *   
	 **/
	function __construct(){
		$this->oe_includes_path = DIR_FS_CATALOG.DIR_WS_INCLUDES;
		$this->oe_fun_path = DIR_FS_CATALOG.DIR_WS_FUNCTIONS;
		$this->load = isset($_REQUEST[$this->ws_request]) ? $_REQUEST[$this->ws_request] : 'default'; 
		$this->db = array(	"name" => DB_DATABASE, 
									"server"=>DB_SERVER,
									"username"=>DB_SERVER_USERNAME,
									"password"=>DB_SERVER_PASSWORD );
		if($this->load != 'default'){
			//Set Variables used as constants through out the web services system
			$this->load_class_file = self::ws_api_dir.self::ws_prefix.$this->load.'.php'; //-------> File contianing the class definition for requested service
			$this->ws_path_sys = DIR_FS_CATALOG.self::ws_app_dir;	//----> System Path to directory containing webservice 
			$this->ws_path_url = self::ws_host.DIR_WS_CATALOG.self::ws_app_dir; //------------> URI Path

			//Require the file containing the class definition of the requested service
			require_once($this->ws_path_sys.$this->load_class_file);
			require_once($this->oe_fun_path.self::oe_database_functions);
			require_once($this->oe_fun_path.self::oe_password_funcs);
			require_once($this->oe_includes_path.self::oe_events_database_tables);			
		}
	}
	

	function db_connect(){
		$link=mysql_connect($this->db["server"],$this->db["username"],$this->db["password"]);
		if (!$link){
			//log_error("1000",sprintf(TEXT_CANNOT_CONNECT,$this->db["server"],$this->db["username"],$this->db["password"]));
			return false;
		} else {
			if (!mysql_select_db($this->db["name"])){
				//log_error("1001",sprintf(TEXT_SELECT_DB,$this->db["name"]));
				return false;
			} else {
				return $link;
			}
		}
	}
	// function to execute query
	function db_query($query,$link){
		$result=mysql_query($query,$link);
			if (!$result){
			//log_error(1002,sprintf(TEXT_EXECUTE_QUERY,mysql_error($link)));
			return false;
		} else {
			return $result;
		}
	}
	// function to give no of rows
	function db_num_rows($query){
		$rows=mysql_num_rows($query);
		return $rows;
	}
	// function to fetch details of current record
	function db_fetch_array($query){
		$result=mysql_fetch_assoc($query);
		return $result;
	}
	// function to fetch Web service value from configuration table
	function ws_get_config(){
		$link = $this->db_connect();
		if ($link) 
			$web_service_sql="select * from configuration where configuration_key='ENABLE_WEB_SERVICE' ";
		else
			return "Failed to Connect to Database";
		$web_service_query=$this->db_query($web_service_sql,$link);
		if($web_service_query)
		{
			if($this->db_num_rows($web_service_query)>0)
			{
				$web_service_result=$this->db_fetch_array($web_service_query);
				define('ENABLE_WEB_SERVICE',$web_service_result['configuration_value']);
				//Set the Server up for dealing with XML and wsdl
				ini_set("soap.wsdl_cache_enabled", "0");
				header('Content-Type: text/xml; charset=UTF-8');
				
				return ENABLE_WEB_SERVICE;
			}
		}
		else
			return "Failed to execute Query";
	}		

	} // END WebService_base

// Use this function to include a function writen in a file that you can not include directly
// It is very important that you use this method rather then copying the function to 
// ensure code changes to the core Freeway system are incorporated in the web service
function require_function($filename, $function){
	$handle = @fopen($filename, "r");
	if ($handle) {
		$fileContents = file($filename, FILE_SKIP_EMPTY_LINES);
		fclose($handle);
	}
	else
		return "handle false";
	if($fileContents != false){
		foreach($fileContents as  $key => $line){
			if(strpos($line, $function) !== false){
				$start_line = $key;
				break;
			}
		}
		$temp = array_slice($fileContents, $start_line + 1,count($fileContents),true);
		foreach($temp as  $key2 => $line2){
			if(strpos($line2,"function") !== false){
				$end_line = $key2;
				break;
			}
		}
		$length = $end_line - $start_line;
		$filePortion = array_slice($fileContents, $start_line, $length);
		$function_text = strstr(strrev(strstr(strrev(implode( PHP_EOL, $filePortion)), "}")),'function');
		eval($function_text);
	}
}
require_function('/var/www/vhosts/iwsdev.com/eden/freeway/includes/functions/general.php', 'tep_sanitize_string');
require_function('/var/www/vhosts/iwsdev.com/eden/freeway/includes/functions/general.php', 'function tep_not_null');
require_function('/var/www/vhosts/iwsdev.com/eden/freeway/includes/functions/general.php', 'function tep_rand(');
require_function('/var/www/vhosts/iwsdev.com/eden/freeway/includes/functions/general.php', 'tep_session_is_registered');



function require_define($filename, $define){
	$handle = @fopen($filename, "r");
	if ($handle) {
		$fileContents = file($filename, FILE_SKIP_EMPTY_LINES);
		fclose($handle);
	}
	else
		return "handle false";
	if($fileContents != false){
		foreach($fileContents as  $key => $line){
			if(strpos($line, $define) !== false){
				$start_line = $key;
				break;
			}
		}
		eval($fileContents[$start_line]);
	}
}

require_define('/var/www/vhosts/iwsdev.com/eden/freeway/includes/database_tables.php', 'TABLE_CUSTOMERS_TEMP_BASKET');
require_define('/var/www/vhosts/iwsdev.com/eden/freeway/includes/database_tables.php', 'TABLE_CUSTOMERS_TEMP_BASKET_SESSIONS');
require_define('/var/www/vhosts/iwsdev.com/eden/freeway/includes/database_tables.php', 'TABLE_CUSTOMERS_BASKET_SESSIONS');
require_define('/var/www/vhosts/iwsdev.com/eden/freeway/includes/database_tables.php', 'TABLE_CUSTOMERS_BASKET');
require_define('/var/www/vhosts/iwsdev.com/eden/freeway/includes/database_tables.php', 'TABLE_CUSTOMERS_INFO');
require_define('/var/www/vhosts/iwsdev.com/eden/freeway/includes/database_tables.php', 'TABLE_ADDRESS_BOOK');
require_define('/var/www/vhosts/iwsdev.com/eden/freeway/includes/database_tables.php', 'TABLE_CUSTOMERS');
require_define('/var/www/vhosts/iwsdev.com/eden/freeway/includes/database_tables.php', 'TABLE_EVENTS');





?>